using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using IFWiki.Models;
using System.Text.RegularExpressions;

namespace IFWiki.Controllers
{
    public class ArticleController : Controller
    {
        //
        // GET: /Article/
        public ActionResult Index(string name)
        {
            name = name ?? "IF Wiki";
            var article = new Article(name);

            return View(article);
        }


        public ActionResult Talk(string name, string innerType)
        {
            innerType = innerType ?? "Article";
            name = "Talk on " + innerType + " " + (name ?? "IF Wiki");

            return RedirectToAction("Index", new { name = name });
        }

        public ActionResult UnTalk(string name)
        {

            var matches = Regex.Matches(name, "Talk on ([a-zA-Z]+) ");
            name = name.Substring(matches[0].Groups[0].Length);

            return RedirectToAction("Index", matches[0].Groups[1].Value, new { name = name });
        }

        public ActionResult ViewSource(string name)
        {
            name = name ?? "IF Wiki";
            var article = new Article(name);
            return View(article);
        }

        public ActionResult ListRevisions(string name)
        {
            name = name ?? "Index";
            //TempData["article"] =  new Article(name);
            //return RedirectToAction("List", "History", new { historyItemName = "article" });
            return RedirectToAction("List", "History", new { name = name, innerType = "Article" });
        }

        //
        // GET: /Article/Edit/5
        public ActionResult Edit(string name)
        {
            name = name ?? "IF Wiki";
            var article = new Article(name);
            return View(article);
        }

        //
        // POST: /Article/Edit/5
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(string name, FormCollection collection)
        {
            name = name ?? "IF Wiki";
            var article = new Article(name);

            try
            {
                // TODO: update article from collection
                string reason = collection["Reason"]; //TODO: Get reason from Form Collection
                article.Detail = collection["Detail"];
                article.Save(reason, User.Identity.Name);

                return RedirectToAction("Index", new { name = article.Name } );
            }
            catch
            {
                return View(article);
            }
        }

        //
        // GET: /Article/Edit/5
        public ActionResult Revert(string name, DateTime revisedAt)
        {
            string revertedBy = User.Identity.Name;
            var article = Article.Revert(name, revisedAt, revertedBy);
            return RedirectToAction("ViewSource", new { name = article.Name });
        }

        public ActionResult ViewRevision(string name, DateTime revisedAt)
        {
            string revertedBy = User.Identity.Name;
            var article = Article.LoadRevision(name, revisedAt);
            return View(article);
        }


    }
}
